using B.S.RbacData.ErrorCode;
using ContentmanagementBaseWrite.API.Applications.Commands.Drugstores;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseWrite.API.Controllers
{
    /// <summary>
    /// 药店管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class DrugstoreController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<DrugstoreController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public DrugstoreController(IMediator mediator, ILogger<DrugstoreController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增药店
        /// </summary>
        /// <param name="command">新增药店命令</param>
        /// <returns>新增结果</returns>
        [HttpPost("add")]
        public async Task<ApiResult<int>> AddDrugstore([FromBody] AddNewDrugstoreCommand command)
        {
            try
            {
                _logger.LogInformation("接收到新增药店请求，药店名称：{PharmacyName}", command.PharmacyName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("药店新增请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "药店新增请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"药店新增异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 更新药店
        /// </summary>
        /// <param name="command">更新药店命令</param>
        /// <returns>更新结果</returns>
        [HttpPost("update")]
        public async Task<ApiResult<int>> UpdateDrugstore([FromBody] UpdateDrugstoreCommand command)
        {
            try
            {
                _logger.LogInformation("接收到更新药店请求，药店ID：{Id}，药店名称：{PharmacyName}", command.Id, command.PharmacyName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("药店更新请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "药店更新请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"药店更新请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 批量删除药店
        /// </summary>
        /// <param name="command">批量删除药店命令</param>
        /// <returns>删除结果</returns>
        [HttpPost("batch-delete")]
        public async Task<ApiResult<int>> BatchDeleteDrugstore([FromBody] BatchDeleteDrugstoreCommand command)
        {
            try
            {
                _logger.LogInformation("接收到批量删除药店请求，药店ID列表：{DrugstoreIds}", string.Join(",", command.DrugstoreIds));

                var result = await _mediator.Send(command);

                _logger.LogInformation("药店批量删除请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "药店批量删除请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"药店批量删除请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }
    }
}